home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue29 / buffstrm / TSTBUFST.DPR < prev   
Encoding:
Text File  |  1997-11-09  |  1.8 KB  |  88 lines

  1. program tstbufst;
  2.  
  3. {$IFDEF Win32}
  4. {$APPTYPE CONSOLE}
  5. {$ENDIF}
  6.  
  7. uses
  8.   {$IFDEF Windows}
  9.   WinCrt,
  10.   WinTypes, WinProcs,
  11.   {$ELSE}
  12.   Windows,
  13.   {$ENDIF}
  14.   Classes,
  15.   BUFFSTRM;
  16.  
  17. type
  18.   PBlock = ^TBlock;
  19.   TBlock = array [0..8191] of byte;
  20.  
  21. var
  22.   StartChar    : char;
  23.   BytesInBlock : integer;
  24.   Block        : PBlock;
  25.  
  26.  
  27. procedure WriteBlock(S : TStream);
  28. begin
  29.   FillChar(Block^, BytesInBlock, StartChar);
  30.   inc(StartChar);
  31.   S.Write(Block^, BytesInBlock);
  32. end;
  33.  
  34. procedure ReadCheckBlock(S : TStream);
  35. var
  36.   i     : integer;
  37. begin
  38.   S.Read(Block^, BytesInBlock);
  39.   for i := 0 to pred(BytesInBlock) do
  40.     if (Block^[i] <> ord(StartChar)) then
  41.       writeln('ERROR at byte ', i);
  42.   inc(StartChar);
  43. end;
  44.  
  45. var
  46.   S : TStream;
  47.   i : longint;
  48.   StartTime : longint;
  49.  
  50. begin
  51.   BytesInBlock := 40;
  52.   GetMem(Block, BytesInBlock);
  53.   try
  54.     writeln('creating the file stream');
  55.     S := TbfsBufferedFileStream.Create('Test', fmCreate, 4096);
  56.     {S := TFileStream.Create('Test', fmCreate);}
  57.     try
  58.       StartTime := GetTickCount;
  59.       writeln('writing blocks');
  60.       StartChar := 'A';
  61.       for i := 0 to 159999 do
  62.         WriteBlock(S);
  63.       writeln('seek to start');
  64.       S.Seek(0, soFromBeginning);
  65.       writeln('readin/checking blocks');
  66.       StartChar := 'A';
  67.       for i := 0 to 159999 do
  68.         ReadCheckBlock(S);
  69.       writeln('readin/checking blocks in reverse order');
  70.       S.Seek(0, soFromEnd);
  71.       for i := 0 to 159999 do begin
  72.         S.Seek(-BytesInBlock, soFromCurrent);
  73.         dec(StartChar);
  74.         ReadCheckBlock(S);
  75.         S.Seek(-BytesInBlock, soFromCurrent);
  76.         dec(StartChar);
  77.       end;
  78.       writeln('Time taken: ', GetTickCount-StartTime);
  79.     finally
  80.       S.Destroy;
  81.     end;
  82.     writeln('done');
  83.     readln;
  84.   finally
  85.     FreeMem(Block, BytesInBlock);
  86.   end;
  87. end.
  88.